#include "StdAfx.h"
#include "JUpateAntiMalware.h"
#include "BDUpdateServiceInterface.h"
#include <process.h>

JUpateAntiMalware* pSelf = NULL;


int Callback(int iE, void* lpData, void * pCtx)
{
	if (NULL == pSelf)
		return 0;

	CString Log;

	//	printf("Event received = %d [%p] [%p]\n", iE, lpData, pCtx);
	if (iE != BD_UPD_EVENT_UPD_DOWNLOADED_BYTES)
		Log.Format("Event received = %03d - ", iE);

	switch (iE)
	{
	case BD_UPD_EVENT_UPD_START:
		{
			Log.Format("Update started");
			SendMessage(pSelf->GetParent(), UM_UPDATING, (WPARAM)lpData, (LPARAM)iE);
			break;
		}
	case BD_UPD_EVENT_UPD_DONE:
		{
			//Log.Format("Update done [%d]", guDownloaded);
			break;
		}
	case BD_UPD_EVENT_UPD_NONE:
		{
			//Log.Format("Update none [%d]", guDownloaded);
			break;
		}
	case BD_UPD_EVENT_UPD_FILE_BEGIN:
		{
			Log.Format("Downloading %s", (char*)lpData);
			SendMessage(pSelf->GetParent(), UM_UPDATING, (WPARAM)lpData, (LPARAM)iE);
			break;
		}
	case BD_UPD_EVENT_UPD_FILE_END:
		{
			Log.Format("File downloaded size %d", *(size_t*)lpData);
			SendMessage(pSelf->GetParent(), UM_UPDATING, (WPARAM)lpData, (LPARAM)iE);
			break;
		}
	case BD_UPD_EVENT_UPD_TOTAL_UPDATE_SIZE:
		{
			Log.Format("Size to download %d", *(size_t*)lpData);
			SendMessage(pSelf->GetParent(), UM_UPDATING, (WPARAM)lpData, (LPARAM)iE);
			break;
		}
	case BD_UPD_EVENT_UPD_DOWNLOADED_BYTES:
		{
			Log.Format("Downloaded %d", *(size_t*)lpData);
			//guDownloaded = *(size_t*)lpData;
			break;
		}
	case BD_UPD_EVENT_UPD_NR_FILES_TO_INSTALL:
		{
			//Log.Format("Files to install = %d\n", *(int*)lpData);
			//nrFilesToInstall = *(int*)lpData;
			//SendMessage(pSelf->GetParent(), UM_UPDATING, (WPARAM)lpData, (LPARAM)iE);
			break;
		}
	case BD_UPD_EVENT_UPD_MSG202:
	case BD_UPD_EVENT_UPD_IO_TIMEOUT:
		{
			//if (!currentFile)
			//{
			//	iRetries = 0;
			//	currentFile = _strdup((char*)lpData ? (char*)lpData : "");
			//}
			//else
			//{
			//	if (lpData && strcmp(currentFile, (char *)lpData))
			//	{
			//		iRetries = 0;
			//		free(currentFile);
			//		currentFile = _strdup((char *)lpData ? (char*)lpData : "");
			//	}
			//	else
			//		iRetries++;
			//}
			//if (iRetries == 3)
			//{
			//	Log.Format("\n");
			//	return BD_UPD_RET_0;
			//}
			//else
			//{
			//	Log.Format("\n");
			//	return BD_UPD_RET_Retry;
			//}
			//
			
			break;
		}
#ifdef WIN32
	case BD_UPD_EVENT_UPD_DOWNLOADINGTORRENT1:
		Log.Format("Downloading torrent 1 %s", (char*)lpData);
		break;
	case BD_UPD_EVENT_UPD_DOWNLOADINGTORRENT7:
		Log.Format("Downloading torrent 7 %s", (char*)lpData);
		break;
	case BD_UPD_EVENT_UPD_STARTDOWNLOADINGZIP1:
		Log.Format("Start downloading zip1 - size %d", *(int *)lpData);
		break;
	case BD_UPD_EVENT_UPD_PROGRESSDOWNLOADINGZIP1:
		Log.Format("Downloading zip1 - downloaded %d", *(int *)lpData);
		break;
	case BD_UPD_EVENT_UPD_ENDDOWNLOADINGZIP1:
		Log.Format("End downloading zip1 - size %d", *(int *)lpData);
		break;
	case BD_UPD_EVENT_UPD_STARTDOWNLOADINGZIP7:
		Log.Format("Start downloading zip7 - size %d", *(int *)lpData);
		break;
	case BD_UPD_EVENT_UPD_PROGRESSDOWNLOADINGZIP7:
		Log.Format("Downloading zip7 - downloaded %d", *(int *)lpData);
		break;
	case BD_UPD_EVENT_UPD_ENDDOWNLOADINGZIP7:
		Log.Format("End downloading zip7 - size %d", *(int *)lpData);
		break;
	case BD_UPD_EVENT_UPD_P2PERROR:
		Log.Format("P2P error no %d", *(int *)lpData);
		break;
#endif
	}

	if (iE != BD_UPD_EVENT_UPD_DOWNLOADED_BYTES)
		Log.Format("\n");
	return 0;
}




JUpateAntiMalware::JUpateAntiMalware(HWND hWnd)
{
	m_hWnd = hWnd;
}


JUpateAntiMalware::~JUpateAntiMalware(void)
{
}



bool JUpateAntiMalware::Update()
{

	pSelf = this;

	bool result = false;
	HMODULE hLib = NULL;
	BDUPDSDKCREATE  BDUpdSdkCreate  = NULL;
	BDUPDSDKDESTROY BDUpdSdkDestroy = NULL;	
	BDUpdateServiceInterface * pUpdater = NULL;
	
	hLib = LoadLibraryW(L"BDUpdateServiceCom.dll");
	if (!hLib)
		goto clean_up;


	BDUpdSdkCreate = (BDUPDSDKCREATE)GetProcAddress(hLib, "BDUpdSdkCreate");
	if (!BDUpdSdkCreate)
		goto clean_up;
		
	pUpdater = (BDUpdateServiceInterface *) BDUpdSdkCreate();

	BDUpdSdkDestroy = (BDUPDSDKDESTROY)GetProcAddress(hLib, "BDUpdSdkDestroy");
	if (!BDUpdSdkDestroy)
		goto clean_up;

	
	int iRet;
	iRet = pUpdater->SetOption(BD_UPD_Server, "https://upgrade.bitdefender.com/av32bit");
	if (iRet != BD_UPD_RET_0)
		goto clean_up;

	iRet = pUpdater->SetOption(BD_UPD_Callback,(void *)Callback);
	if (iRet != BD_UPD_RET_0)
		goto clean_up;

	int value = 1;
	pUpdater->SetOption(BD_UPD_UseVersionId, (const void*)&value);

	const int iLocalPathLen = 256;
	WCHAR wLocalPath[iLocalPathLen];
	GetModuleFileNameW(NULL, wLocalPath, iLocalPathLen);
	WCHAR* wEnd = wcsrchr(wLocalPath, L'\\');
	wEnd[0] = 0;
	//swprintf(wLocalPath, L"%s\\Engine", wLocalPath);
	iRet = pUpdater->SetOption(BD_UPD_Local_Path,(void *)wLocalPath);
	if (iRet != BD_UPD_RET_0)
		goto clean_up;

	iRet = pUpdater->PerformUpdate();
	if (iRet != BD_UPD_RET_0)
		goto clean_up;
	
	pUpdater->PerformInstall();

	result = true;

clean_up:
	if (pUpdater && BDUpdSdkDestroy)
		BDUpdSdkDestroy(pUpdater);

	if (hLib)
		FreeLibrary(hLib);


	SendMessage(pSelf->GetParent(), UM_UPDATING, 0, BD_UPD_EVENT_UPD_NR_FILES_TO_INSTALL);

	return result;
}

